<?php
/**
 * @software         [JiJinCMS]
 * @author           [上海集锦信息科技有限公司]
 * @website          [http://www.jijinweb.com]
 * @email            [sunny@jijinweb.com]
 **/
namespace app\admin\controller;
use think\facade\View;
use app\admin\model\RoleModel;
use think\facade\Db;
use think\facade\Request;
class Ad extends \app\admin\controller\Admin{
    public function __construct(\think\App $app) {
        parent::__construct($app);
        $this->model=new \app\admin\model\AdModel();
        $adCat=Db::name("ad_cat")->order("sort asc,catid asc")->select();
        View::assign("adCat",$adCat);
        View::assign("pubid","20210423145046");//这里pubid设置为固定的，因为广告内容很少图片建议通用
    }
    //
    public function index(){
        $where=[];
        $catid=Request::param("catid",0,"intval");
        if($catid){
            $where["catid"]=$catid;
        }
        $data=$this->model->where($where)->order("sort asc,id asc")->select();
        foreach($data as $k=>$v){
            $data[$k]['cat']=Db::name("ad_cat")->find($v['catid']);
        }
        View::assign("catid",$catid);
        View::assign("data",$data);
        return View::fetch();
    }
    //
    public function add(){
        if(request()->isPost()){
            $data=Request::param();
            $data['status']=1;
            $validate = \think\facade\Validate::rule(['name'=>'require'])->message([
                "name.require"=>"请填写广告名称",
            ]);
            if (!$validate->check(Request::param())) {
                $this->error($validate->getError());
            }
            $this->model->save($data);
            $lastId=$this->model->id;
            //
            $this->UserLogModel->add(Request()->action(),$this->model->getName(),$lastId,$this->request->param());
            $this->success("添加成功");
        }else{
            $catid=Request::param("catid",0,"intval");
            View::assign("catid",$catid);
            return View::fetch();
        }
    }
    public function update(){
        if(request()->isPost()){
            $data=Request::param();
            $validate = \think\facade\Validate::rule(['name'=>'require'])->message([
                "name.require"=>"请填写广告名称",
            ]);
            if (!$validate->check(Request::param())) {
                $this->error($validate->getError());
            }
            $this->model->update($data);
            $this->UserLogModel->add(Request()->action(),$this->model->getName(),$data['id'],$this->request->param());
            $this->success("修改成功",url('index'));

        }else{
            $id=Request::param("id",0,"intval");
            $r=$this->model->find($id);
            View::assign("r",$r);
            return View::fetch();
        }
    }
    //
    public function delete(){
        $ids=Request::param("check_id");
        if(!$ids){
            $this->error('请选择信息！');
            return false;
        }
        foreach($ids as $id){
            $this->model->destroy($id);
        }
        $this->UserLogModel->add(Request()->action(),$this->model->getName(),$ids,$this->request->param());
        $this->success("删除成功");
    }
    //设置状态是否启用
    public function setStatus(){
        $id=Request::param('id',0,"intval");
        $status=Request::param('status',0,"intval");
        $this->model->update(['id'=>$id,'status'=>$status]);
        //记录日志
        $this->UserLogModel->add(Request()->action(),$this->model->getName(),$id);        
        $this->success("设置成功");
    }       
    //字段排序
    public function sort(){
        $ids=Request::param("ids");
        $sort=Request::param("sort");
        foreach($ids as $key=>$id){
            $data['id']=(int)$id;
            $data['sort']=(int)$sort[$key];
            $this->model->update($data);
        }
        $this->success("排序更改成功");
    }
}
